#define _CRT_SECURE_NO_WARNINGS 1
#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define endl '\n'
const int N = 1e6 + 10;
int a[N],c[N];
ll b, ans;

int main() {
	ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
	int n; cin >> n;
	for (int i = 1; i <= n; i++) {
		cin >> a[i];
		b += a[i];
	}
	b /= n;
	for (int i = 2; i <= n; i++) {
		c[i] = c[i - 1] + a[i - 1] - b;
	}
	sort(c + 1, c + n + 1);
	for (int i = 1; i <= n; i++) {
		ans += abs(c[i] - c[(n + 1) / 2]);
	}
	cout << ans << endl;
	return 0;
}